package org.school.controller;

import lombok.extern.log4j.Log4j2;
import org.school.entity.Dingdan;
import org.school.service.DingdanService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * (Dingdan)表控制层
 *
 * @author makejava
 * @since 2023-01-14 13:57:08
 */
@RestController
@RequestMapping("dingdan")
@CrossOrigin
@Log4j2
public class DingdanController {
    /**
     * 服务对象
     */
    @Resource
    private DingdanService dingdanService;

    /**
     * 分页查询
     *
     * @param dingdan 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @GetMapping
    public ResponseEntity<Page<Dingdan>> queryByPage(Dingdan dingdan, PageRequest pageRequest) {
        return ResponseEntity.ok(this.dingdanService.queryByPage(dingdan, pageRequest));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<Dingdan> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.dingdanService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param dingdan 实体
     * @return 新增结果
     */
    @PostMapping
    public ResponseEntity<Dingdan> add(Dingdan dingdan) {
        return ResponseEntity.ok(this.dingdanService.insert(dingdan));
    }

    /**
     * 编辑数据
     *
     * @param dingdan 实体
     * @return 编辑结果
     */
    @PutMapping
    public ResponseEntity<Dingdan> edit(Dingdan dingdan) {
        return ResponseEntity.ok(this.dingdanService.update(dingdan));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public ResponseEntity<Boolean> deleteById(Integer id) {//根据id删除菜
        return ResponseEntity.ok(this.dingdanService.deleteById(id));
    }

}

